<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class ShopController extends Controller
{
    //add 添加页
    public function add()
    {
    	$title = '后台店铺管理';
    	return view('admin.shop.add',['title'=>$title]);
    }

    //insert 添加处理
    public function insert(Request $request)
    {
    	$data = $request -> except('_token');
    	$data['time']=date('Y-m-d H:i:s',time());
    	// dd($data);

    	//限制条件
    	$this ->validate($request, [
    			'shopname' => 'required|max:30',
    			'sellerid' => 'required',
    			'goodshop' => 'required',
    			'status' => 'required',
    			'logo'=>'required',
    		],[
    			'shopname.required' => '店铺名称不能为空',
    			'shopname.max' => '店铺名称不能超过30个字符长度',
    			'sellerid.required' => '店主id不能为空',
    			'logo.required'=> '店铺logo不能为空',
    		]);

    	//处理店铺logo
		if($request -> hasFile('logo'))
		{
			if($request -> file('logo') -> isValid())
			{
				//获取后缀名
				$suffix = $request -> file('logo') -> getClientOriginalExtension();
				// echo $suffix;
				//随机文件名
				$fileName = time().mt_rand(100000,999999).'.'.$suffix;
				// $path = date('Ymd');
				$path = './uploads';
				$request -> file('logo') -> move($path,$fileName);

				$data['logo'] = trim($path.'/'.$fileName,'.');
				// echo $data['logo'];
			}
		}

		//向数据库添加数据
		// dd($data);
    	$res = DB::table('shop') -> insert($data);

    	// 判断
		if($res)
		{
			return redirect('/admin/shop/index') -> with('success','添加成功');
		}else
		{
			return back() -> with('error','添加失败');
		}
    }

    //列表页
    public function index(Request $request)
    {
    	$title = '店铺列表页';
    	//搜索分页
    	$data = DB::table('shop')
    			-> where(function($query)use($request)
    				{$query->where('shopname','like','%'.$request->input('keywords').'%');})
    			->paginate($request ->input('num',10));
    	// dd($data);
    	return view('admin.shop.index', ['title'=>$title, 'data'=>$data, 'request'=>$request->all()]);
    }

    //编辑页
    public function edit($id)
    {
    	// dd($id);
    	$data=DB::table('shop')->where('id',$id)->first();
    	// dd($data);
    	$title = '后台编辑页';
    	return view('admin.shop.edit',['title'=>$title,'data'=>$data]);
    }

    public function update(Request $request)
    {
    	$data = $request->except('_token','oldlogo');
    	//限制条件
    	$this ->validate($request, [
    			'shopname' => 'required|max:30',
    			'sellerid' => 'required',
    			'goodshop' => 'required',
    			'status' => 'required',
    		],[
    			'shopname.required' => '店铺名称不能为空',
    			'shopname.max' => '店铺名称不能超过30个字符长度',
    			'sellerid.required' => '店主id不能为空',
    		]);
    	// dd($data);

    	//如果有图片被修改 也就是有图片上传时
    	if($request -> hasFile('logo'))
		{
			if($request -> file('logo') -> isValid())
			{
				//获取后缀名
				$suffix = $request -> file('logo') -> getClientOriginalExtension();
				// echo $suffix;
				//随机文件名
				$fileName = time().mt_rand(100000,999999).'.'.$suffix;
				// echo $fileName;
				$path = './uploads';
				$request -> file('logo') -> move($path,$fileName);

				$data['logo'] = trim($path.'/'.$fileName,'.');
				// echo $data['logo'];
				 
				//老图片的路径
				$oldpath = '.'.$request -> input('oldlogo');
				//删除老图片
				if(file_exists($oldpath))
				{
					unlink($oldpath);
				}
			}
		}
		

		//在数据库中修改
		// dd($data);
		$res = DB::table('shop')->where('id',$data['id'])->update($data);	
		//判断更新是否成功=
    	if($res)
		{
			return redirect('/admin/shop/index') -> with(['success' => '更新成功']);
		}else{
			return back() -> with(['error' => '您尚未更新任何信息或更新失败']);
		}
    }

    //delete
    public function delete($id)
    {
    	// dd($id);
    	$res=DB::table('shop')->where('id',$id)->delete();
    	if($res)
		{
			return redirect('/admin/shop/index') -> with(['success' => '删除成功']);
		}else{
			return back() -> with(['error' => '删除失败']);
		}
    }

}
